package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.bean.OftenAreaBean;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/4/19
 * Desc:
 */
public class OftenAreaViewBinderNew extends ItemViewBinder<OftenAreaBean, OftenAreaViewBinderNew.ViewHolder> {
    private OnIconClickListener onIconClickListener;

    public void setOnIconClickListener(OnIconClickListener onIconClickListener) {
        this.onIconClickListener = onIconClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_often_order_area, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull OftenAreaBean item) {
        if (item.isChangBg()) {
            holder.img_bg.setVisibility(View.VISIBLE);
            if (item.getBgUrl() != null) {
                ImageLoader.getInstance().displayImage(item.getBgUrl(), holder.img_bg, options);
            }
        } else {
            holder.img_bg.setVisibility(View.GONE);
        }


        holder.txt_often_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onIconClickListener != null) {
                    //常购清单
                    onIconClickListener.onIconClick(0);
                }
            }
        });
        holder.txt_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onIconClickListener != null) {
                    //订单
                    onIconClickListener.onIconClick(1);
                }
            }
        });
        holder.txt_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onIconClickListener != null) {
                    //关注
                    onIconClickListener.onIconClick(2);
                }
            }
        });
        holder.txt_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onIconClickListener != null) {
                    //台账
                    onIconClickListener.onIconClick(3);
                }
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img_bg;
        private TextView txt_often_list, txt_order, txt_attention, txt_account;

        ViewHolder(View itemView) {
            super(itemView);
            img_bg = itemView.findViewById(R.id.img_bg);
            txt_often_list = itemView.findViewById(R.id.txt_often_list);
            txt_order = itemView.findViewById(R.id.txt_order);
            txt_attention = itemView.findViewById(R.id.txt_attention);
            txt_account = itemView.findViewById(R.id.txt_account);
        }
    }

    public interface OnIconClickListener {
        void onIconClick(int type);//0：常购清单；1：订单；2：关注；3：台账
    }

    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.home_rv_list_bg) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.home_rv_list_bg)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.home_rv_list_bg)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

}
